package com.guys8.cemsbackend.controller;


import com.guys8.cemsbackend.DTO.OutschoolInformation;
import com.guys8.cemsbackend.DTO.Response;
import com.guys8.cemsbackend.DTO.UserInformation;
import com.guys8.cemsbackend.entity.Outschool;
import com.guys8.cemsbackend.entity.User;
import com.guys8.cemsbackend.service.impl.OutschoolServiceImpl;
import com.guys8.cemsbackend.service.impl.UserServiceImpl;
import com.guys8.cemsbackend.utils.JWTutil.JWTUtil;
import com.guys8.cemsbackend.utils.UploadUtil.UploadUtil;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * (Outschool)表控制层
 *
 * @author breeze
 * @since 2022-08-25 09:50:55
 */
@RestController
@Api(tags=" Outschool-信息相关接口")
@RequestMapping("/Outschool")
public class OutschoolController {
    /**
     * 服务对象
     */
    @Autowired
    private OutschoolServiceImpl outschoolService;
    @Autowired
    private UserServiceImpl userService;


    @PostMapping("/addOutschool/{outTime}&&{outCause}&&{where}&&{backTime}&&{note}")
    @ApiOperation("上传离校申请信息(token)")
    public Response addOutSchool(@RequestHeader String schoolid, @PathVariable Date outTime, @PathVariable String outCause,
                                 @PathVariable String where, @PathVariable Date backTime, MultipartFile note) {
        schoolid = JWTUtil.getUsername(schoolid);

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);

        String notePath = "src/main/resources/note/"+schoolid+"/";
        Outschool outschool=new Outschool(user.getUserId(),outTime,outCause,where,notePath,backTime);
        outschoolService.save(outschool);

        Integer outid = outschoolService.getusernowOut(user.getUserId()).getOutId();
        if(UploadUtil.uploadnotefile(schoolid, String.valueOf(outid),note)){
            return Response.restResult(1);
        }else{
            return Response.restResult(0);
        }

    }

    @DeleteMapping("/logoutOutschoolBySchoolId/")
    @ApiOperation("删除一用户最近上传的个离校信息(token)")
    public Response deleteOutSchool(@RequestHeader String schoolid) {
        schoolid = JWTUtil.getUsername(schoolid);

        User user=userService.getbyschoolid(schoolid);
        if(user == null)return Response.restResult(0);
        Outschool outschool=outschoolService.getusernowOut(user.getUserId());
        if(outschool!=null) {
            if(outschoolService.removeById(outschoolService.getusernowOut(user.getUserId()).getOutId())){
                return Response.restResult(1);
            }
        }
        return Response.restResult(0);
    }

    @GetMapping("/getBySchoolid/")
    @ApiOperation("按token查找最近的离校信息(token)")
    public Response getOutSchoolbyToken(@RequestHeader String schoolid){
        schoolid = JWTUtil.getUsername(schoolid);
        return getOutSchool(schoolid);
    }

    @PostMapping("/getBySchoolid/find/{schoolid}")
    @ApiOperation("按schoolId查找最近的离校信息")
    public Response getOutSchool(@PathVariable String schoolid){
        User user=userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        UserInformation userIn = new UserInformation(user);
        Outschool outschool=outschoolService.getusernowOut(user.getUserId());
        return Response.restResult(1,userIn,new OutschoolInformation(user,outschool));
    }

    @GetMapping("/getAllBySchoolid/")
    @ApiOperation("按Token查找所有的离校信息(token)")
    public Response getALLOutSchoolbyToken(@RequestHeader String schoolid){
        schoolid = JWTUtil.getUsername(schoolid);
        return getALLOutSchool(schoolid);
    }

    @PostMapping("/getAllBySchoolid/find/{schoolid}")
    @ApiOperation("按schoolId查找所有的离校信息")
    public Response getALLOutSchool(@PathVariable String schoolid){
        User user=userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        UserInformation userIn = new UserInformation(user);
        List<Outschool> outschools=outschoolService.getallById(user.getUserId());
        List<OutschoolInformation> outschoolInformations = new LinkedList<>();
        for(Outschool outschool:outschools){
            outschoolInformations.add(new OutschoolInformation(user,outschool));
        }
        return Response.restResult(1,userIn,outschoolInformations);
    }

    @GetMapping("/getById/{OutschoolId}")
    @ApiOperation("//按Id查找信息(数据库测试展示接口)")
    public OutschoolInformation getoneById(@PathVariable Integer OutschoolId) {
        Outschool outschool = outschoolService.getById(OutschoolId);
        User user = userService.getById(outschool.getUserId());
        return new OutschoolInformation(user,outschool);
    }

}

